package cn.yunyichina.provider.finance.creator.journal;

import cn.yunyichina.provider.finance.creator.FinanceAccountJournalCreator;
import cn.yunyichina.provider.finance.entity.AccountJournalPo;
import cn.yunyichina.provider.finance.entity.BoneOrderBo;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.commons.DateUtils;
import cn.yunyichina.utils.commons.IDUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuxp on 2017/12/28.
 *  求助订单流水写入
 * @param
 * @Descrition
 */
@Log4j
public class SeekHelpOrderJournal implements FinanceAccountJournalCreator {

    private static final String BUSINESS_DESC = "求助单服务费";

    private BoneOrderBo boneOrderBo;

    /**
     * 求助单流水
     *
     * @param boneOrderBo
     */
    public SeekHelpOrderJournal(BoneOrderBo boneOrderBo) {
        super();
        this.boneOrderBo = boneOrderBo;
    }

    @Override
    public List<AccountJournalPo> createJournal() throws Exception {
        log.info("即将生成求助单流水");
        List<AccountJournalPo> list = new ArrayList<>();

        // 主治（有主治）
        String professorDoctorId = boneOrderBo.getProfessorDoctorId();
        if (StringUtils.isNotBlank(professorDoctorId)) {
            log.info("该求助单有主治医生");
            AccountJournalPo professorAccountJournalPo = new AccountJournalPo();
            professorAccountJournalPo.setId(IDUtils.getUUID32());
            professorAccountJournalPo.setOrderNo(boneOrderBo.getOrderNo());
            professorAccountJournalPo.setRelationNo(boneOrderBo.getRelationNo());
            professorAccountJournalPo.setOrderType(boneOrderBo.getOrderType());

            // 查询主治医生的信息（机构，科室）
            Response response = BaseService.callHumpFromInternal(
                    "yyt.base.orguser.relation.get", "{\"user_id\":\""
                            + professorDoctorId + "\"}");
            String result = response.getResult();
            if (StringUtils.isBlank(result)) {
                log.error("生成押金单（有主治）流水异常，未查询到主治医生信息 userId: " + professorDoctorId);
                throw new Exception("生成押金单（有主治）流水异常，未查询到主治医生信息 userId: " + professorDoctorId);
            }

            log.info("主治医生信息: " + result);
            JSONObject resultMap = JSON.parseObject(result);
            professorAccountJournalPo.setOrgId(resultMap.getString("orgId"));
            professorAccountJournalPo.setOrgCode(resultMap.getString("orgCode"));
            professorAccountJournalPo.setOrgName(resultMap.getString("orgName"));
            professorAccountJournalPo.setDeptId(resultMap.getString("deptId"));
            professorAccountJournalPo.setDeptCode(resultMap.getString("deptCode"));
            professorAccountJournalPo.setDeptName(resultMap.getString("deptName"));
            professorAccountJournalPo.setUserId(professorDoctorId);
            professorAccountJournalPo.setUserName(resultMap.getString("userName"));
            professorAccountJournalPo.setBusinessDesc(BUSINESS_DESC);
            professorAccountJournalPo.setAmountDirection(Short.valueOf("1"));
            professorAccountJournalPo.setAmount(boneOrderBo.getProfessorDoctorRecvAmount());
            professorAccountJournalPo.setBusinessType((short) 1);
            professorAccountJournalPo.setCreatedTime(DateUtils.getDateTimeStr());
            list.add(professorAccountJournalPo);
        }

        // 专家
        String appointmentDoctorId = boneOrderBo.getAppointmentDoctorId();
        AccountJournalPo appointmentAccountJournalPo = new AccountJournalPo();
        appointmentAccountJournalPo.setId(IDUtils.getUUID32());
        appointmentAccountJournalPo.setOrderNo(boneOrderBo.getOrderNo());
        appointmentAccountJournalPo.setRelationNo(boneOrderBo.getRelationNo());
        appointmentAccountJournalPo.setOrderType(boneOrderBo.getOrderType());

        // 查询专家的信息（机构，科室）
        Response response = BaseService.callHumpFromInternal(
                "yyt.base.orguser.relation.get", "{\"user_id\":\""
                        + appointmentDoctorId + "\"}");
        String result = response.getResult();
        if (StringUtils.isBlank(result)) {
            log.error("生成求助单（有主治）流水异常，未查询到专家信息 userId: " + appointmentDoctorId);
            throw new Exception("生成求助单（有主治）流水异常，未查询到专家信息 userId: " + appointmentDoctorId);
        }

        log.info("专家信息: " + result);
        JSONObject resultMap = JSON.parseObject(result);
        appointmentAccountJournalPo.setOrgId(resultMap.getString("orgId"));
        appointmentAccountJournalPo.setOrgCode(resultMap.getString("orgCode"));
        appointmentAccountJournalPo.setOrgName(resultMap.getString("orgName"));
        appointmentAccountJournalPo.setDeptId(resultMap.getString("deptId"));
        appointmentAccountJournalPo.setDeptCode(resultMap.getString("deptCode"));
        appointmentAccountJournalPo.setDeptName(resultMap.getString("deptName"));
        appointmentAccountJournalPo.setUserId(appointmentDoctorId);
        appointmentAccountJournalPo.setUserName(resultMap.getString("userName"));
        appointmentAccountJournalPo.setBusinessDesc(BUSINESS_DESC);
        appointmentAccountJournalPo.setAmountDirection(Short.valueOf("1"));
        appointmentAccountJournalPo.setAmount(boneOrderBo.getAppointmentDoctorRecvAmount());
        appointmentAccountJournalPo.setBusinessType((short) 4);
        appointmentAccountJournalPo.setCreatedTime(DateUtils.getDateTimeStr());
        list.add(appointmentAccountJournalPo);

        // 平台
        AccountJournalPo platformAccountJournalPo = new AccountJournalPo();
        platformAccountJournalPo.setId(IDUtils.getUUID32());
        platformAccountJournalPo.setOrderNo(boneOrderBo.getOrderNo());
        platformAccountJournalPo.setRelationNo(boneOrderBo.getRelationNo());
        platformAccountJournalPo.setOrderType(boneOrderBo.getOrderType());
        platformAccountJournalPo.setOrgId(boneOrderBo.getPlatformId());
        platformAccountJournalPo.setOrgCode(boneOrderBo.getPlatformCode());
        platformAccountJournalPo.setOrgName(boneOrderBo.getPlatformName());
        platformAccountJournalPo.setBusinessDesc("求助平台服务费");
        platformAccountJournalPo.setAmountDirection(Short.valueOf("1"));
        platformAccountJournalPo.setAmount(boneOrderBo.getPlatformServiceFee());
        platformAccountJournalPo.setBusinessType((short) 19);
        platformAccountJournalPo.setCreatedTime(DateUtils.getDateTimeStr());
        list.add(platformAccountJournalPo);

        return list;
    }
}
